package com.cskaoyan.market.controller.wx;


import com.cskaoyan.market.annotation.FlashList;
import com.cskaoyan.market.bo.WxOrderCommentBo;
import com.cskaoyan.market.bo.WxOrderListBo;
import com.cskaoyan.market.bo.WxOrderSubmitBo;
import com.cskaoyan.market.db.domain.MarketOrderGoods;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.FlashMapFactory;
import com.cskaoyan.market.service.WxOrderService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.WxOrderDetailVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;

    @Autowired
    FlashMapFactory flashMapFactory;
    @Autowired
    SecurityManager securityManager;

    @GetMapping("list")
    public Object list( WxOrderListBo wxOrderListBo){
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();


        Object resultList = wxOrderService.list(user,wxOrderListBo);

        return resultList;
    }
        @FlashList(runningType = "after",method = "order")
    @GetMapping("detail")
    public Object detail(Integer orderId){

        WxOrderDetailVo detail = wxOrderService.detail(orderId);


        return ResponseUtil.ok(detail);
    }

    @FlashList(runningType = "after",method = "order")
    @PostMapping("refund")
    public Object refund(@RequestBody Map<String,Integer> map){
        Integer orderId = map.get("orderId");
        Integer refund = wxOrderService.updateOrder(orderId,(short)202);
        if(refund == 0){
            return ResponseUtil.ok();
        }


       return ResponseUtil.fail();
    }

    @FlashList(runningType = "after",method = "order")
    @PostMapping("cancel")
    public Object cancel(@RequestBody Map<String,Integer> map){
        Integer orderId = map.get("orderId");
        Integer refund = wxOrderService.updateOrder(orderId,(short)102);
        if(refund == 0){
            return ResponseUtil.ok();
        }


        return ResponseUtil.fail();
    }

    @FlashList(runningType = "after",method = "order")
    @PostMapping("delete")
    public Object delete(@RequestBody Map<String,Integer> map){
        Integer orderId = map.get("orderId");
        Integer refund = wxOrderService.updateOrder(orderId,null);
        if(refund == 0){
            return ResponseUtil.ok();
        }


        return ResponseUtil.fail();
    }

    @FlashList(runningType = "after",method = "order")
    @PostMapping("confirm")
    public Object confirm(@RequestBody Map<String,Integer> map){
        Integer orderId = map.get("orderId");
        Integer refund = wxOrderService.updateOrder(orderId,(short)401);
        if(refund == 0){
            return ResponseUtil.ok();
        }


        return ResponseUtil.fail();
    }

    @FlashList(runningType = "after",method = "order")
    @PostMapping("prepay")
    public Object prepay(@RequestBody Map<String,Integer> map){
        Integer orderId = map.get("orderId");
        Integer refund = wxOrderService.updateOrder(orderId,(short)201);
        if(refund == 0){
            return ResponseUtil.ok();
        }


        return ResponseUtil.fail();
    }

    @FlashList(runningType = "after",method = "order")
    @GetMapping("goods")
    public Object goods(Integer orderId,Integer goodsId){

   MarketOrderGoods marketOrderGoods= wxOrderService.goods(orderId,goodsId);

        return ResponseUtil.ok(marketOrderGoods) ;
    }
//    @RequestBody Map<String,Object> map
    /*
    * content: "asdasd"
hasPicture: false
orderGoodsId: 2
picUrls: []
star: 5*/

@FlashList(runningType = "after",method = "order")
    @PostMapping("comment")
    public Object comment(@RequestBody Map<String,Object> map){
//        String content,Boolean hasPicture,Integer orderGoodsId, String[] picUrls, Short star
        WxOrderCommentBo wxOrderCommentBo = new WxOrderCommentBo();
//        wxOrderCommentBo.setContent((String) map.get("content"));
//        wxOrderCommentBo.setHasPicture((Boolean) map.get("hasPicture"));
//        wxOrderCommentBo.setOrderGoodsId((Integer) map.get("orderGoodsId"));
//        String[] picUrls=(String[]) map.get("picUrls");
//
//        wxOrderCommentBo.setPicUrls(picUrls);
//        wxOrderCommentBo.setStar((Short) map.get("star"));
        String content = (String) map.get("content");
        Boolean hasPicture = (Boolean) map.get("hasPicture");
        List<String> picUrlsList = (List<String>) map.get("picUrls");

        String[] picUrls = picUrlsList.toArray(new String[0]);

        Integer orderGoodsId = (Integer) map.get("orderGoodsId");
        int star = (int) map.get("star");
        wxOrderCommentBo.setContent(content);
            wxOrderCommentBo.setHasPicture(hasPicture);
            wxOrderCommentBo.setOrderGoodsId(orderGoodsId);
            wxOrderCommentBo.setPicUrls(picUrls);
            wxOrderCommentBo.setStar((short)star);
        Integer code = wxOrderService.comment(wxOrderCommentBo);
        if(code == 0){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    @FlashList(runningType = "after",method = "order")
    @PostMapping("submit")
    public Object submit(@RequestBody WxOrderSubmitBo wxOrderSubmitBo){

        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        Integer userId = user.getId();
        Integer orderId = wxOrderService.submit(wxOrderSubmitBo,userId);

//        grouponLinkId: 0
//        orderId: 242
        Map<String,Integer> data = new HashMap<>();
        data.put("grouponLinkId",0);
        data.put("orderId",orderId);


        return ResponseUtil.ok(data);
    }

//   public Integer getUserId(){
//
//      Integer userId=  wxOrderService.getUserID();
//
//
//        return userId;
//   }
}
